﻿using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;

namespace 原生SQL;

public partial class NetTest2Context : DbContext
{
    public NetTest2Context()
    {
    }

    public NetTest2Context(DbContextOptions<NetTest2Context> options)
        : base(options)
    {
    }

    public virtual DbSet<TArticle> TArticles { get; set; }

    public virtual DbSet<TComment> TComments { get; set; }

    public virtual DbSet<TDelivery> TDeliveries { get; set; }

    public virtual DbSet<TLeaf> TLeaves { get; set; }

    public virtual DbSet<TOrder> TOrders { get; set; }

    public virtual DbSet<TSale> TSales { get; set; }

    public virtual DbSet<TStudent> TStudents { get; set; }

    public virtual DbSet<TTeacher> TTeachers { get; set; }

    public virtual DbSet<TUser> TUsers { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see https://go.microsoft.com/fwlink/?LinkId=723263.
    {
        optionsBuilder.UseSqlServer("Server=.;Database=NetTest2;Trusted_Connection=True;TrustServerCertificate=True;MultipleActiveResultSets=true");
        //简单日志：在控制台显示EF Core生成的SQL
        //optionsBuilder.LogTo(Console.WriteLine);
    }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<TArticle>(entity =>
        {
            entity.ToTable("T_Articles");

            entity.Property(e => e.Title).HasMaxLength(100);
        });

        modelBuilder.Entity<TComment>(entity =>
        {
            entity.ToTable("T_Comments");

            entity.HasIndex(e => e.ArticleId, "IX_T_Comments_ArticleId");

            entity.HasOne(d => d.Article).WithMany(p => p.TComments).HasForeignKey(d => d.ArticleId);
        });

        modelBuilder.Entity<TDelivery>(entity =>
        {
            entity.ToTable("T_Deliveries");

            entity.HasIndex(e => e.OrderId, "IX_T_Deliveries_OrderId").IsUnique();

            entity.HasOne(d => d.Order).WithOne(p => p.TDelivery).HasForeignKey<TDelivery>(d => d.OrderId);
        });

        modelBuilder.Entity<TLeaf>(entity =>
        {
            entity.ToTable("T_Leaves");

            entity.HasIndex(e => e.ApproverId, "IX_T_Leaves_ApproverId");

            entity.HasIndex(e => e.RequesterId, "IX_T_Leaves_RequesterId");

            entity.HasOne(d => d.Approver).WithMany(p => p.TLeafApprovers).HasForeignKey(d => d.ApproverId);

            entity.HasOne(d => d.Requester).WithMany(p => p.TLeafRequesters).HasForeignKey(d => d.RequesterId);
        });

        modelBuilder.Entity<TOrder>(entity =>
        {
            entity.ToTable("T_Orders");
        });

        modelBuilder.Entity<TSale>(entity =>
        {
            entity.HasKey(e => e.Id).HasName("PK__T_Sales__3214EC0748CFD27E");

            entity.ToTable("T_Sales");

            entity.Property(e => e.Price).HasColumnType("numeric(10, 2)");
            entity.Property(e => e.WareName).HasMaxLength(30);
        });

        modelBuilder.Entity<TStudent>(entity =>
        {
            entity.ToTable("T_Students");

            entity.HasMany(d => d.Teachers).WithMany(p => p.Students)
                .UsingEntity<Dictionary<string, object>>(
                    "TStudentsTeacher",
                    r => r.HasOne<TTeacher>().WithMany().HasForeignKey("TeachersId"),
                    l => l.HasOne<TStudent>().WithMany().HasForeignKey("StudentsId"),
                    j =>
                    {
                        j.HasKey("StudentsId", "TeachersId");
                        j.ToTable("T_Students_Teachers");
                        j.HasIndex(new[] { "TeachersId" }, "IX_T_Students_Teachers_TeachersId");
                    });
        });

        modelBuilder.Entity<TTeacher>(entity =>
        {
            entity.ToTable("T_Teachers");
        });

        modelBuilder.Entity<TUser>(entity =>
        {
            entity.ToTable("T_Users");
        });

        OnModelCreatingPartial(modelBuilder);
    }

    partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}
